package com.link510.aitools.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.link510.aitools.R;
import com.link510.aitools.core.helper.Network2kUtils;
import com.link510.aitools.core.listen.CWMListener;
import com.link510.aitools.framework.activity.BaseAcitivity;
import com.link510.aitools.services.CustomWaitDialogUtils;
import com.link510.aitools.services.NettyManager;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.OnClick;

public class Login2Activity extends BaseAcitivity {

    @BindView(R.id.core_login_button)
    Button LoginButton;

    @BindView(R.id.corebroad_ip_addr)
    TextView corebroadIpAddrTextView;

    private CustomWaitDialogUtils customWaitDialogUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        customWaitDialogUtils = new CustomWaitDialogUtils(mContext);
        ToastUtils.cancel();
        searchCoreBroad();
    }

    @OnClick(R.id.reload_search_button)
    public void reloadSearchButtonClick() {
        searchCoreBroad();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.robot_core_login2_topmenu, menu);
        return super.onCreateOptionsMenu(menu);
    }


    // 菜单的监听方法
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.network_test_item) {
            Intent intent = new Intent(Login2Activity.this, NetWorkTestActivity.class);
            Login2Activity.this.startActivity(intent);
        }

        return true;
    }

    /**
     * 搜索核心板
     */
    private void searchCoreBroad() {

        new Thread(() -> {

            Login2Activity.this.runOnUiThread(() -> {
                customWaitDialogUtils.show("智能卡口扫描中...");
            });

            try {
                Network2kUtils.getInstance().searchCorbroadIP();
            } finally {

                Login2Activity.this.runOnUiThread(() -> {
                    Login2Activity.this.LoginButton.setVisibility(View.VISIBLE);
                    corebroadIpAddrTextView.setText(Network2kUtils.getInstance().getLocalCoreBroadIpAddr());
                    customWaitDialogUtils.close();
                });

            }
        }).start();
    }

    @OnClick(R.id.core_login_button)
    public void loginButtonClick() {

        if (!NettyManager.getInstance().init()) {
            ToastUtils.showShort("网络初始化失败...");
            return;
        }

        NettyManager.getInstance().connect(Network2kUtils.getInstance().getLocalCoreBroadIpAddr(),
                5050,
                new CWMListener<Void>() {
                    @Override
                    public void onSuccess(String message, Void v) {

                        ToastUtils.showShort("服务启动成功...");
                        try {
                            Logger.e("initTcpClient:" + message);
                        } catch (Exception ex) {
                            Logger.e("设备注册失败: ");
                        }
                    }

                    @Override
                    public void onError(int code, String message) {

                        ToastUtils.showShort("服务启动失败...");
                    }
                });

    }

    @OnClick(R.id.corebroad_ip_addr)
    public void corebroadIpAddrClick() {

        ToastUtils.showShort("ok");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customWaitDialogUtils != null) {
            customWaitDialogUtils.dismiss();
        }
    }
}